package com.ruoyi.system.service.impl;

import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class SmsService {

    @Value("${aliyun.sms.access-key-id}")
    private String accessKeyId;

    @Value("${aliyun.sms.access-key-secret}")
    private String accessKeySecret;

    @Value("${aliyun.sms.sign-name}")
    private String signName;

    @Value("${aliyun.sms.template-code}")
    private String templateCode;

    public boolean sendSms(String phoneNumber, String templateParam) {
        try {
            // 创建Aliyun短信API客户端
            IClientProfile profile = DefaultProfile.getProfile("cn-changchun", accessKeyId, accessKeySecret);
            DefaultAcsClient client = new DefaultAcsClient(profile);

            // 创建发送短信请求
            SendSmsRequest request = new SendSmsRequest();
            request.setPhoneNumbers(phoneNumber); // 收件人号码
            request.setSignName(signName); // 短信签名
            request.setTemplateCode(templateCode); // 短信模板编号
            request.setTemplateParam(templateParam); // 短信模板变量

            // 发送短信
            SendSmsResponse response = client.getAcsResponse(request);

            // 判断发送是否成功
            return "OK".equals(response.getCode());
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
    }
}
